package com.direction.hold.ui;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TabHost;
import android.widget.TextView;
import app.base.ui.NS;
import cn.cheln.explorer.actions.OperationUtil;
import cn.cheln.explorer.beans.ApkInfo;
import cn.cheln.explorer.beans.FileInfo;
import cn.cheln.explorer.config.Config;
import cn.cheln.explorer.config.Constants;
import cn.cheln.explorer.ds.database.SQLManager;
import cn.cheln.explorer.ds.uri.NavigationConstants;
import cn.cheln.explorer.func.AppSrc;
import cn.cheln.support.data.text.xml.SPUtil;
import cn.cheln.support.log.Logs;
import cn.cheln.support.notification.NotifyUtil;
import cn.cheln.support.v4.pagerui.PageInfo;
import cn.cheln.support.v4.pagerui.ViewPagerHelper;
import cn.cheln.support.v4.pagerui.ViewPagerIndicator;
import cn.cheln.support.v4.view.PagerView;
import cn.power.shine.R;

import com.direction.hold.VPAty;

public class SlideActivity extends Activity {
	// /** Called when the activity is first created. */
	public static final int TAB_INDEX_CATEGORY = 0;
	public static final int TAB_INDEX_SDCARD = 1;
	public static SlideActivity paramActivity;
	public static VPAty fileActivityInstance;
	public static boolean firstTimeLaunch = true;
	public int titleHeight;
	public int isInOperation = -1;
	public TabHost mTabHost;
	public TextView sdcardView;
	public int mDetailType;
	public int currFrameId;
	public static VPAty activity;
	boolean distributeFCFeed = false;
	boolean launchPathActivity = false;
	private ArrayList<PageInfo> pageInfos;
	public ViewPagerHelper viewPagerHelper;

	public SlideActivity(VPAty activity) {
		this.activity = activity;
	}

	public static PageInfo genPageInfo(Class<? extends Activity> activityClz,
			Context context, Intent intent, Class clazz) {
		PageInfo sdcardpageInfo;
		sdcardpageInfo = new PageInfo(context, null, null, null, intent,
				activityClz, clazz.getName());
		return sdcardpageInfo;
	}

	// public static ApkNotifyBroadcast apkNotifyBroadcast;

	public void postCreate(Bundle bundle) throws Exception {

		new Thread() {

			@Override
			public void run() {

				AppSrc.initInstalledApps(fileActivityInstance);
				// PackageInstallManager.createPackageManagerInstance(
				// new InstallHelper(fileActivityInstance),
				// fileActivityInstance);
				OperationUtil.initStrCnst(fileActivityInstance);
				FileInfo.init(fileActivityInstance);
				ApkInfo.init(fileActivityInstance);

				NotifyUtil
						.cancel(fileActivityInstance, NotifyUtil.ID_INSTALLED);
				Boolean pic_loaded = null;

				try {
					Config.versioncode = fileActivityInstance
							.getPackageManager().getPackageInfo(
									NS.nameSpace,
									PackageManager.GET_CONFIGURATIONS).versionCode;
					pic_loaded = (Boolean) SPUtil
							.getValue(
									fileActivityInstance,
									Constants.SharedPrefernce.File,
									Constants.SharedPrefernce.KEY_UPDATE_LAODING_PIC + 10,
									Boolean.class);
				if (Config.versioncode != -1
						&& Config.versioncode > 10
						&& pic_loaded==null
										|| !pic_loaded) {

					new OperationUtil().recursiveDelete(new File(
							Constants.Thumb_folder));
					SPUtil.putValue(
							fileActivityInstance,
							Constants.SharedPrefernce.File,
							Constants.SharedPrefernce.KEY_UPDATE_LAODING_PIC + 10,
							true);
				}} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}.start();
		if (distributeFCFeed || launchPathActivity) {
			// fileActivityInstance.findViewById(R.id.searchBtn).setVisibility(
			// View.GONE);
			// fileActivityInstance.findViewById(R.id.searchBtnLeftLine)
			// .setVisibility(View.GONE);

		}

		fileActivityInstance.findViewById(R.id.searchBtn).setOnClickListener(
				new OnClickListener() {

					public void onClick(View arg0) {
						Intent intent = new Intent(fileActivityInstance,
								SearchActivity.class);
						intent.putExtra("isNew", true);
						fileActivityInstance.startActivity(intent);
					}
				});
	}

	public void endJob() {
		// RuntimeArg.shouldStopInstall = true;
		// PackageInstallManager.getInstance().postJobWhenExiting(
		// (Integer)Res.getAttr("R.string.install_stoped_by_app_exit"));
		// new Timer().schedule(new TimerTask() {
		//
		// @Override
		// public void run() {
		// NotifyHelper.cancelApkNOtification();
		// }
		// }, 2000);
	}

	public void setEntryActivity(VPAty activity) {
		this.fileActivityInstance = activity;
	}

	public void preCreate(final VPAty explorerApp) throws Exception {
		NS.init(explorerApp);
		paramActivity = this;
		SQLManager.createFileDB(explorerApp);

		Logs.setLevel(Config.LOG_LEVEL);
		NavigationConstants.init(explorerApp);
	}

	public void setSwipCapability(VPAty activity) {
		activity.setContentView(R.layout.main);
		pageInfos = new ArrayList<PageInfo>(0);
		pageInfos.add(genPageInfo(CountActivity.class, activity,
				activity.getIntent(), activity.getClass()));
		pageInfos.add(genPageInfo(PathActivity.class, activity,
				activity.getIntent(), activity.getClass()));
		viewPagerHelper = new ViewPagerHelper(activity, null);
		viewPagerHelper.setup(activity, 0, pageInfos);
	}

	public void setSwipTitle(final VPAty activity) {
		final ViewPagerIndicator indicator = (ViewPagerIndicator) activity
				.findViewById(R.id.indicator);
		ViewPagerIndicator.PageInfoProvider titleprovider = new ViewPagerIndicator.PageInfoProvider() {

			@Override
			public String getTitle(int paramInt) {
				switch (paramInt) {
				case 0:
					return activity.getString(R.string.tab_category);
				case 1:
					return activity.getString(R.string.tab_sdcardlist);
				}
				return "";
			}
		};
		indicator.init(0, 2, titleprovider, 169, 0);

		indicator
				.setOnTriggerListener(new ViewPagerIndicator.OnPagerSlidingListener() {
					@Override
					public void onChangePagerTrigger(int paramInt) {
						indicator.updatePositions(paramInt);
					}
				});
		((PagerView) activity.findViewById(R.id.pager))
				.setOnPageChangeListener(indicator);
	}

	// public void setSwipeFeature(FileApp activity) {
	// Intent intent = activity.getIntent();
	// String action = intent.getAction();
	// if ("com.lewa.filemgr.count_start".equals(action)) {
	// distributeFCFeed = true;
	// }
	// if (Constants.InvokedPath.ACTION_INVOKED_PATH.equals(action)) {
	// launchPathActivity = true;
	// }
	// ArrayList<StartParameter> aClasses = new ArrayList<StartParameter>();
	//
	// if (!launchPathActivity) {
	// Intent countIntent;
	// if (distributeFCFeed) {
	// countIntent = intent;
	// } else {
	// countIntent = new Intent();
	// }
	// countIntent.putExtra("delayloadcontent", true);
	// aClasses.add(new StartParameter(CountActivity.class, countIntent,
	// R.string.tab_category));
	// }
	//
	// if (!distributeFCFeed) {
	// Intent sdcardIntent;
	// if (launchPathActivity) {
	// sdcardIntent = intent;
	// } else {
	// sdcardIntent = new Intent();
	// }
	// sdcardIntent.putExtra("delayloadcontent", true);
	// aClasses.add(new StartParameter(PathActivity.class, sdcardIntent,
	// R.string.tab_sdcardlist));
	// }
	// // Intent privacyIntent = new Intent();
	// // privacyIntent.putExtra("delayloadcontent", true);
	// // aClasses.add(new StartParameter(PrivacyActivity.class, privacyIntent,
	// // R.string.tab_privacy));
	//
	// activity.setupFlingParm(aClasses, R.layout.main, R.id.indicator,
	// R.id.pager);
	// // openIntent(getIntent());
	//
	// }

	public void onEventDestroy() {
		endJob();

	}

	public void setPage(int i) {
		viewPagerHelper.getCurrentPage(i);
	}

	public void cancelApkNotification() {
		NotifyUtil.cancel(fileActivityInstance, NotifyUtil.ID_INSTALLED);
	}

}
